﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Script.Serialization;

namespace AjaxControlFramework
{
	public class DotNetJsonCallbackSerializer : CallbackSerializer
	{
        //------// Fields \\------------------------------------------------\\
        public static readonly Regex JavascriptDateRegex = new Regex("\"\\\\/Date\\((\\d+)\\)\\\\/\"");
        //------\\ Fields //------------------------------------------------//



		//------// Methods \\-----------------------------------------------\\
		public override string SerializeCallbackResult(Dictionary<string, object> callbackResult)
		{
			string jsonResult = new JavaScriptSerializer(new SimpleTypeResolver()).Serialize(callbackResult);
            return JavascriptDateRegex.Replace(jsonResult, "new Date($1)");
		}


        public override string SerializeException(Exception exception)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new JavaScriptConverter[] { new ExceptionJavascriptConverter() });
            return serializer.Serialize(exception);
        }


		public override T DeserializeCallbackArguments<T>(string strObj)
		{
			return new JavaScriptSerializer(new SimpleTypeResolver()).Deserialize<T>(strObj);
		}
		//------\\ Methods //-----------------------------------------------//
	}
}
